//
//
//  cell输入刷新高度
//

#import "CJCustomNameTextViewEditedTableViewCell.h"
#define SCREEN  [UIScreen mainScreen].bounds.size
#define ScreenW kScreenWidth
#define TitleLableWidth 71

@interface CJCustomNameTextViewEditedTableViewCell ()<CJBaseTextViewDelegate>
@property (nonatomic, strong) UILabel *flagLable;//标记lable

@end

@implementation CJCustomNameTextViewEditedTableViewCell {
    UILabel * titleLabel;
    CGFloat _contentHeight;
    NSString *_title;
    
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier withTitleName:(NSString *)title withFlagShow:(BOOL)show{
    self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _title = title;
        self.isFlag = show;
        [self uiConfigure];
        
    }
    return self;
}
- (void)uiConfigure {
    
    if (self.isFlag) {
        UILabel *flagLable = [[UILabel alloc] init];
        flagLable.textColor = [UIColor colorWithHexString:@"#ff5a3f"];
        flagLable.text = @"*";
        flagLable.font = [UIFont systemFontOfSize:14.f];
        [self.contentView addSubview:flagLable];
        self.flagLable = flagLable;
        [flagLable mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(15);
            make.top.equalTo(self.contentView).offset(16);
            make.width.equalTo(@6.5);
        }];
    }
    
    if (![CJUIKit isNullWithString:_title]) {
        titleLabel=[[UILabel alloc] init];
        titleLabel.text = _title;
        titleLabel.font = [UIFont systemFontOfSize:14.f];
        titleLabel.textColor = [UIColor colorWithHexString:kNomalTextBlackColor];
        [self.contentView addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            if (self.isFlag) {
                make.left.equalTo(self.flagLable.mas_right);
            }else{
                make.left.equalTo(self.contentView).offset(15);
            }
            
            
            make.top.equalTo(self.contentView).offset(16);
            make.width.equalTo(@TitleLableWidth);
        }];
    }
    
    
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.contentView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.contentView);
        make.bottom.equalTo(self.contentView);
        make.height.equalTo(@0.5);
    }];
    
    _lineView = lineView;
    
    
    
    
    CJBaseTextView *textView=[[CJBaseTextView alloc]init];
    textView.delegateTextView=self;
    textView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    textView.textColor = [UIColor colorWithHexString:kDetailTextBlackColor];
    textView.font = [UIFont systemFontOfSize:14.f];
    textView.scrollEnabled=NO;
    
    textView.returnKeyType=UIReturnKeyDone;
    textView.keyboardType=UIKeyboardTypeDefault;
    textView.backgroundColor=[UIColor clearColor];
    textView.placeholder = @"请输入内容";
    [self.contentView addSubview:textView];
    self.textView = textView;
    [textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(8);
        if ([CJUIKit isNullWithString:self->_title]) {
            make.left.equalTo(self.contentView).offset(15);
        }else{
            make.left.equalTo(self->titleLabel.mas_right).offset(8);
        }
        
        make.right.equalTo(self.contentView).offset(-15);
        make.height.equalTo(@34);
        make.bottom.mas_greaterThanOrEqualTo(lineView.mas_top).offset(-8);
        
    }];
    
}



- (void)setContentStr:(NSString *)contentStr {
    
    _contentStr=contentStr;
    if (contentStr.length == 0) {
        self.textView.placeHolderLable.hidden = NO;
    }else{
        self.textView.placeHolderLable.hidden = YES;
    }
    self.textView.text = _contentStr;
    CGSize constraintSize;
    if ([CJUIKit isNullWithString:_title]) {
        constraintSize = CGSizeMake(ScreenW - 2* 15, MAXFLOAT);
    }else{
        if (self.isFlag) {
            constraintSize = CGSizeMake(SCREEN.width - 2* 15 - TitleLableWidth   - 8 - 6.5, MAXFLOAT);
        }else{
            constraintSize = CGSizeMake(SCREEN.width - 15 - TitleLableWidth  - 16, MAXFLOAT);
        }
        
    }
    
    CGSize size = [self.textView sizeThatFits:constraintSize];
    _contentHeight = size.height;
    NSLog(@"%f",_contentHeight);
    [self.textView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(size.height));
    }];
}



- (void)setPlaceholder:(NSString *)placeholder {
    _placeholder=placeholder;
    self.textView.placeholder= _placeholder;
}
- (CGFloat)CellHeight {
    CGFloat resultH = _contentHeight + 8 + 8  +0.5;
    if (resultH <= 67.5) {
        return 67.5;
    }else{
        return resultH;
    }
    
}


- (void)CJ_textViewDidChange:(UITextView *)textView{
    CGSize constraintSize;
    if ([CJUIKit isNullWithString:_title]) {
        constraintSize = CGSizeMake(ScreenW - 2* 15, MAXFLOAT);
    }else{
        if (self.isFlag) {
            constraintSize = CGSizeMake(SCREEN.width - 2* 15 - TitleLableWidth   - 8 - 6.5, MAXFLOAT);
        }else{
            constraintSize = CGSizeMake(SCREEN.width - 15 - TitleLableWidth  - 16, MAXFLOAT);
        }
        
    }
    
    CGSize size = [textView sizeThatFits:constraintSize];
    _contentHeight = size.height;
    NSLog(@"%f",_contentHeight);
    [self.textView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(size.height));
    }];
    if ([self.delegate respondsToSelector:@selector(updatedText:)]) {
        [self.delegate updatedText:textView.text];
        
    }
    
    
    [_mytableView beginUpdates];
    [_mytableView endUpdates];
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
